package com.dl7.downloaderlib;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;


/**
 * Created by long on 2015/11/17.
 * 下载配置
 */
public class DownloadConfig {

    // 下载目录
    private String mDownloadDir;
    private int mRetryTimes;

    // 下载目录文件保存
    private static final String STORAGE_KEY = "DownloadDir";

    /**
     * @date: 2019/8/2 0002
     * @author: gaoxiaoxiong
     * @description:获取外部存储目录下的 fileName的文件夹路径
     **/
    public String getDiskFileDir(Context context, String fileName){
        String cachePath = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {//此目录下的是外部存储下的私有的fileName目录
            if (context.getExternalCacheDir() != null) {
                cachePath = context.getExternalFilesDir(fileName).getAbsolutePath();  //mnt/sdcard/Android/data/com.my.app/files/fileName
            }else {
                cachePath = context.getFilesDir().getPath()+ "/" + fileName;        //data/data/com.my.app/files
            }
        } else {
            cachePath = context.getFilesDir().getPath()+ "/" + fileName;        //data/data/com.my.app/files
        }
//        cachePath = context.getFilesDir().getPath()+ "/" + fileName;        //data/data/com.my.app/files
        File file = new File(cachePath);
        if (!file.exists()){
            file.mkdirs();
        }
        return file.getAbsolutePath();  //mnt/sdcard/Android/data/com.my.app/files/fileName
    }


    private DownloadConfig() {
//            mDownloadDir = Environment.getExternalStorageDirectory().getAbsolutePath() +
//                    "/download/";
//
//        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
//                || !Environment.isExternalStorageRemovable()) {
//            String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath();
//            if (!TextUtils.isEmpty(storageDir)){
//                mDownloadDir = Environment.getExternalStorageDirectory().getAbsolutePath() +
//                        "/download/";
//            }else {
//                mDownloadDir = getDiskFileDir(FileDownloader.getContext(), "download/");
//            }
//
//        }
        mDownloadDir = getDiskFileDir(FileDownloader.getContext(), "download") + "/";
            File dir = new File(mDownloadDir);
            if (!dir.exists()) {
                dir.mkdirs();
            } else if (!dir.isDirectory()) {
                dir.delete();
                dir.mkdirs();
            }
            mRetryTimes = 10;
    }

    public String getDownloadDir() {
        return mDownloadDir;
    }

    public int getRetryTimes() {
        return mRetryTimes;
    }


    /**
     * 构建器
     */
    public static class Builder {
        private DownloadConfig config;

        public Builder() {
            config = new DownloadConfig();
        }

        public DownloadConfig build() {
            return config;
        }

        public Builder setDownloadDir(String downloadDir) {
            config.mDownloadDir = downloadDir;
//            PreferencesUtils.putString(FileDownloader.getContext(), STORAGE_KEY, downloadDir);
            return this;
        }

        public Builder setRetryTimes(int retryTimes) {
            config.mRetryTimes = retryTimes;
            return this;
        }
    }

}
